﻿    var parentInfo = {};
    var fromToDate = {
			minDate : '',
			maxDate : ''
		};
    if (typeof(localStorage.userInfo)!="undefined") {
    	var userInfo = JSON.parse(localStorage.userInfo); 
    }
    else{
    	mui.toast("检测不到登录状态，请重新登录！");
    }
    
    window.addEventListener('backIndex', function(event) {
		//自定义方法，刷新数据，不刷新页面
		console.log('back index');
		var lists = plus.webview.all();
		for (var i in lists) {
			if (lists[i].id != 'index') {
				plus.webview.close(lists[i].id);
			}
		}
		getOldList();
//		var jsonStr = event.detail.jsonStr;
	});
    
    var map = new AMap.Map("container", {
    	resizeEnable: true,
    	center: [ 113.325989,22.996340],
    	//地图中心点
    	zoom: 15 //地图显示的缩放级别
    });
    
//途径点
    var lineArr = [];
//  var lineArr = [
//  	[113.325989, 22.996340],
//  	[113.324979, 22.996830],
//  	[113.325985, 22.996850],
//  	[113.325980, 22.996340],
//  	[113.325979, 22.995940],
//  	[113.325989, 22.994340]
//  ];
	var markers = [];
	var markArr =[{'location':''}];
//  var markArr = [
//	    	 '113.325989,22.996330'
//	    ];


	
    var polyline = new AMap.Polyline({
    	path: lineArr,
    	strokeColor: '#3366FF',
    	strokeOpacity: 0.8,
    	strokeWeigth: 1000,
    	strokeStyle: 'dashed',
    	strokeDasharray: [10, 5],
    	geodesic: true
    });
//  polyline.setPath(lineArr);
//	polyline.setMap(map);
    $('#historyLine').on('tap', function() {
    	console.log(63+JSON.stringify(lineArr))
    	polyline.setPath(lineArr);
    	console.log(65+JSON.stringify(lineArr))
    	if (!$(this).hasClass('mui-active')) {
    		$(this).addClass('mui-active');
    		polyline.setMap(map);
    	} else {
    		$(this).removeClass('mui-active');
    		polyline.setMap();
    	}

    });




	
	var showMap = function(){
		for (var i = 0; i < markArr.length; i += 1) {
	    	var marker;
	    	var icon = new AMap.Icon({
	    		image: '../images/user.png',
	    		size: new AMap.Size(30, 30)
	    	});
	    	console.log('222222'+markArr.length);
	    	marker = new AMap.Marker({
	    		icon: icon,
	    		position: markArr[i].location,
	    		offset: new AMap.Pixel(-12, -12),
	    		zIndex: 101,
//	    		title: markArr[i].name,
	    		map: map
	    	});
	
	    	markers.push(marker);
	    }
	    map.setFitView();
	}
	
	
	var askLocation = function(index){
		var dateVal = getDate();
		switch (index){
			case 0: 
				fromToDate.minDate = dateVal[0];
				fromToDate.maxDate = dateVal[2];
				break;
			case 1: 
				fromToDate.minDate = dateVal[2];
				fromToDate.maxDate = dateVal[2];
				break;
			case 2: 
				fromToDate.minDate = dateVal[1];
				fromToDate.maxDate = dateVal[1];
				break;
			case 3: 
				fromToDate.minDate = dateVal[0];
				fromToDate.maxDate = dateVal[0];
				break;
		}
		getLocation(index);
//		return fromToDate;
	}
	
	var getDate = function(){
		var d0 = new Date();
		var d1 = new Date(Date.parse(d0)-86400000);
		var d2 = new Date(Date.parse(d0)-86400000*2);
		
		var day0 = d0.getFullYear().toString() + "-" + (d0.getMonth() + 1) + "-" + d0.getDate();
		var day1 = d1.getFullYear().toString() + "-" + (d1.getMonth() + 1) + "-" + d1.getDate();
		var day2 = d2.getFullYear().toString() + "-" + (d2.getMonth() + 1) + "-" + d2.getDate();
		
		var dateVal = [day2,day1,day0];
		// [22,23,24]
		console.log(dateVal)
		return dateVal;
	}
	
	var getLocation = function(index){
//		askLocation(index);
		var paramsLoc = {
//				oldManId : 1,
//				minDate  : "2017-09-05",
//				maxDate  : "2017-09-06",
				oldManId : parentInfo.oldMenId,
				minDate  : fromToDate.minDate,
				maxDate  : fromToDate.maxDate
			}
		console.log(55+JSON.stringify(paramsLoc))
		App.oldManLocaltion(paramsLoc,function(data){
				console.log(148+JSON.stringify(data));
				if (data.result) {
//				alert(data.data.length)
				if (data.data.length != 0) {
					for (var i in data.data) {
						lineArr[i] = [parseFloat(data.data[i].longitude),parseFloat(data.data[i].latitude)];
					}
				console.log(155+ JSON.stringify(lineArr) );
				markArr[0].location   = lineArr[0];
				map.clearMap();
				showMap();
				polyline.setMap(map);
			}
		}
		});		
	};
	
	var getOldList = function(){
		var params ={
	    		sysChildrenId : userInfo.sysChildrenId
	    	}
	    	App.bingOldManList(params,function(data){
				console.log(JSON.stringify(data));
				if (data.result) {
					if (data.data.length == 0) {
						$('#noPerson').show();
						mui.toast("未绑定老人信息,请前往绑定");
					}
					else{
						localStorage.parentData = JSON.stringify(data.data);
						$('#contactBtn').css('display','block');
						$('#historyLine').css('display','block');
						$('#navBar').css('display','block');
						var oldManIndex = 0;
						if(typeof(localStorage.oldManIndex)!="undefined"){
							oldManIndex = parseInt(localStorage.oldManIndex);
						}
						else{
							localStorage.oldManIndex = 0;
						}
						var fromToDate = askLocation(1);
						parentInfo = data.data[oldManIndex];
//						getLocation(0);
					}
				}
	    	});
	}
	
	
//	    var TestData = {
//	    	result:true,
//	    	data:[
//	    		{
//		    		longitude : 113.345989,
//		    		latitude :22.996340
//	    		},
//	    		{
//		    		longitude : 113.385999,
//		    		latitude :22.996340
//	    		},
//	    		{
//		    		longitude : 113.315989,
//		    		latitude :22.996350
//	    		},
//	    		{
//		    		longitude : 113.395979,
//		    		latitude :22.996320
//	    		},
//	    	]
//	    };
	showMap();   
//  
//  if (TestData.result) {
//  	if (TestData.data.length != 0) {
//  		for (var i in TestData.data) {
//  			var a = [TestData.data[i].longitude, TestData.data[i].latitude];
//  			lineArr.push(a);
//  		}
//  		console.log(268 + lineArr[0]);
//  		markArr[0].location = lineArr[0];
//  		map.clearMap();
//  		showMap();
//  		console.log(lineArr)
//  		polyline.setMap(map);
//  	}
//  }
//
	$('#noPerson').on('tap', function() {
    	bindPage = mui.preload({
    		url: 'bind.html',
    		id: 'bind',
    		styles: {

    		}
    	});
    	setTimeout(function(){
    		App.showPage('bind');
    	},500);
    });

    

    $('#contactBtn').on('tap', function() {
    	contactPage = mui.preload({
    		url: 'contact.html',
    		id: 'contact',
    		styles: {

    		}
    	});
    	setTimeout(function(){
    		App.showPage('contact');
    	},500);
    });

    $('#toPersonCenter').on('tap', function() {
    	personCenterPage = mui.preload({
    		url: 'personCenter.html',
    		id: 'personCenter',
    		styles: {

    		}
    	});
    	setTimeout(function(){
    		App.showPage('personCenter');
    	},1000);
    });
    
    $('#contactUs').on('tap', function() {
    	$('#contactBox').show('fast	');
    });
    
    $('#shutDown').on('tap', function() {
    	$('#contactBox').hide('fast');
    });
    
    $('#nearestThree').on('tap',function(){
    	askLocation(0);
    });
    
    $('#todayPosition').on('tap',function(){
    	askLocation(1);
    });
    
    $('#yesterdayPosition').on('tap',function(){
    	askLocation(2);
    });
    
    $('#qiantianPosition').on('tap',function(){
    	askLocation(3);
    });

    $(function() {　
    	mui.plusReady(function() {
    		//预加载页面
    	});
    	mui.init({
    		gestureConfig: {
    			tap: true,
    			//默认为true
    			doubletap: true,
    			//默认为false
    			longtap: true,
    			//默认为false
    			swipe: true,
    			//默认为true
    			drag: true,
    			//默认为true
    			hold: false,
    			//默认为false，不监听
    			release: false
    		}
    	});

    	var first = null;
    	mui.back = function() {
    		if (!first) {
    			first = new Date().getTime();
    			mui.toast('再按一次退出软件!');
    			setTimeout(function() {
    				first = null;
    			}, 2000);
    		} else {
    			if (new Date().getTime() - first < 2000) {
    				plus.runtime.quit();
    			}
    		}
    	};
    	
    	getOldList();
    	
    	if (typeof(userInfo)!="undefined") {
//  		var paramsPerson ={
//	    		sysChildrenId : userInfo.sysChildrenId
//	    	}
//	    	App.getPersonInfo(paramsPerson,function(data){
//				console.log(1101+JSON.stringify(data));
//			});
			if(typeof(userInfo.trueName) == 'undefined'){
				mui.toast("未完善个人资料请先完善个人资料");
			}
    		
    	}
    	else{
    		mui.toast("检测不到登录状态，请重新登录！");
    	}
	
		

    });